// by Xeno
#define THIS_FILE "fn_make_isle_grp.sqf"
#include "x_setup.sqf"

__TRACE("Starting _make_isle_grp");
private ["_units", "_start_point", "_dc", "_agrp", "_elist", "_vecs", "_rand", "_leader", "_grp_array", "_npos"];
_units = [];
_dc = 0;
_start_point = [GVAR(with_isledefense) select 0,GVAR(with_isledefense) select 1,GVAR(with_isledefense) select 2,GVAR(with_isledefense) select 3] call FUNC(GetRanPointSquare);
#ifndef __TT__
while {(count _start_point < 3 || {(_start_point distance GVAR(FLAG_BASE) < 2000)} || {(_start_point distance _old_old_start < 500)}) && {_dc < 99}} do {
#else
while {(count _start_point == 0 || {(_start_point distance GVAR(WFLAG_BASE) < 1500)} || {(_start_point distance GVAR(EFLAG_BASE) < 1500)} || {_start_point distance _old_old_start < 500}) && {_dc < 99}} do {
#endif
	_start_point = [GVAR(with_isledefense) select 0,GVAR(with_isledefense) select 1,GVAR(with_isledefense) select 2,GVAR(with_isledefense) select 3] call FUNC(GetRanPointSquare);
	sleep 0.353;
	__INC(_dc);
};
_start_point = _start_point call FUNC(WorldBoundsCheck);
__TRACE_1("","_start_point");
sleep 0.01;
_old_old_start = _start_point;
__GetEGrp(_agrp)
_elist = [GVAR(enemy_side)] call FUNC(getmixedliste);
_vecs = [];
_npos = _start_point;
{
	_rand = floor random 2;
	if (_rand > 0) then {
		_reta = [_rand,_npos,_x select 0,_agrp,0,-1.111] call FUNC(makevgroup);
		_vecs = [_vecs, _reta select 0] call FUNC(arrayPushStack2);
		_units = [_units, _reta select 1] call FUNC(arrayPushStack2);
		sleep 0.73;
		_npos = ((_reta select 0) select 0) modelToWorld [0,-12,0];
	};
} forEach _elist;
_elist = nil;
sleep 0.31;

_agrp setVariable [QGVAR(PATR),true];
_agrp setVariable [QGVAR(PATR_ISLE),true];
[_agrp, _start_point, [GVAR(with_isledefense) select 0,GVAR(with_isledefense) select 1,GVAR(with_isledefense) select 2,GVAR(with_isledefense) select 3]] spawn FUNC(MakePatrolWPX);
#ifndef __TT__
if (__XJIPGetVar(GVAR(searchintel)) select 6 == 1) then {
	_agrp spawn FUNC(IsleDefMarkerMove);
};
#endif
[_agrp, _units, [0,0,0], _vecs]